<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

function cache_category( $model )
{
	global $tree;
	$tree = array( 
		array( 
			'img' => '<img src="./images/system/icon/66.gif" />' ,
			'name'=> '文档库', 
			'id' => 0, 
			'pid' => 0, 
			'level' => 0,
			'onepage' => 0,
			'order' => 0,
		) 
	);
	make_category_tree( $model );
	cache_write( 'category_'.$model, $tree );
	return;
}

function make_category_tree( $model, $pid = 0, $end_array = array(), $level = 1 )
{
	global $tree, $db;
	$category = $db->get_where( DB_PRE.'category', array( 'model' => $model, 'pid' => $pid ), array( 'order', 'ASC' ) );
	$cat_no = count( $category );
	foreach( $category as $key => $value )
	{
		$pre = '';
		foreach( $end_array as $k => $v )
		{
			$k+1 <= $level && $pre .= $v == 1?'<img src="./images/system/icon/60.gif" />':'<img src="./images/system/icon/57.gif" />';
		}
		if( $level !== 1 && $value['child'] )
		{
			$img_no = ( $key+1 == $cat_no )?63:61;
			$img = '<img src="./images/system/icon/'.$img_no.'.gif" onclick="global.categoryTree('.$value['id'].')" id="img_'.$value['id'].'" />';
		}
		else
		{
			$img_no = ( $key+1 == $cat_no )?59:58;
			$img = '<img src="./images/system/icon/'.$img_no.'.gif" />';
		}
		$orn = $value['onepage']?68:65;
		$tree[] = array( 
			'img' => $pre.$img.'<img src="./images/system/icon/'.$orn.'.gif" />',
			'name' => $value['name'],
			'id' => $value['id'],
			'pid' => $pid,
			'level' => $level,
			'onepage' => $value['onepage'],
			'order' => $value['order']
		);
		$end_array[$level] = $key+1 !== $cat_no?1:0;
		make_category_tree( $model, $value['id'], $end_array, $level+1 );
	}
}
























function cache_write( $file, $datas, $type='array' )
{
	if( $type == 'array' )
	{
		$string = "<?php !defined('SUNCMS') && exit('No direct script access allowed');\n return ".var_export( $datas, TRUE ).";\n";
	}
	elseif( $type == 'constant' )
	{
		$data='';
		foreach($string as $key => $value)
		{
			$data .= "define('".strtoupper( $key )."','".addslashes( $value )."');\n";
		}
		$string = "<?php\n".$data."\n?>";
	}
	$file = SUNCMS.'datas/cache/'.$file.'.php';
	$write = file_put_contents( $file, $string );
	@chmod( $file, 0777 );
	return $write;
}



/***************************************************************************************************************************/

/**
 * 分类缓存
 *
 */
function cache_article_column()
{
	global $tree;
	$tree = array();
	category_tree();
	cache_write( 'article', 'column', $tree );
	unset( $tree );
}

/**
 * 生成分类树
 *
 * @param int $pid
 * @param int $level
 */
function category_tree( $pid = 0, $level = 1 )
{
	global $tree, $db;
	$sql = "SELECT `id`, `name`, `pid`, `ifshow`, `ifpost`, `comment`, `attribute`, `html_path`, `keywords`, `description`, `order`, `jump`, `index_temp`, `list_temp`, `article_temp`, `view_group`, `post_group` FROM `".DB_PRE."articlecolumn` WHERE `pid` = '$pid' ORDER BY `order` ASC";
	$category = $db->query( $sql );
	foreach( $category as $key=>$value )
	{
		if( $level == 1 )
		{
			$tree[] = array
			(
					'id' => $value['id'],
					'name' => $value['name'],
					'pid' => $value['pid'],
					'ifshow' => $value['ifshow'],
					'ifpost' => $value['ifpost'],
					'comment' => $value['comment'],
					'attribute' => $value['attribute'],
					'html_path' => $value['html_path'],
					'keywords' => $value['keywords'],
					'description' => $value['description'],
					'order' => $value['order'],
					'jump' => $value['jump'],
					'index_temp' => $value['index_temp'],
					'list_temp' => $value['list_temp'],
					'article_temp' => $value['article_temp'],
					'view_group' => $value['view_group'],
					'post_group' => $value['post_group']
			);
			category_tree( $value['id'], $level+1 );
		}
		else
		{
			$tree[] = array
			(
					'id' => $value['id'],
					'name' => str_repeat( '&nbsp;&nbsp;&nbsp;', $level-1 ).'└'.$value['name'],
					'pid' => $value['pid'],
					'ifshow' => $value['ifshow'],
					'ifpost' => $value['ifpost'],
					'comment' => $value['comment'],
					'attribute' => $value['attribute'],
					'html_path' => $value['html_path'],
					'keywords' => $value['keywords'],
					'description' => $value['description'],
					'order' => $value['order'],
					'jump' => $value['jump'],
					'index_temp' => $value['index_temp'],
					'list_temp' => $value['list_temp'],
					'article_temp' => $value['article_temp'],
					'view_group' => $value['view_group'],
					'post_group' => $value['post_group']
			);
			category_tree( $value['id'], $level+1 );
		}
	}
}

function cache_member_group()
{
	global $db;
	$group_1 = $db->query( "SELECT `id`, `name`, `grade`, `upgrade_points` FROM `".DB_PRE."membergroup` WHERE type='user' ORDER BY `id` ASC" );
	$group_2 = $db->query( "SELECT `id`, `name`, `grade`, `upgrade_points` FROM `".DB_PRE."membergroup` WHERE type='admin' ORDER BY `id` ASC" );
	$group = array();
	foreach( $group_1 as $v )
	{
		$group['用户组'][] = $v;
	}
	foreach( $group_2 as $v3 )
	{
		$group['系统组'][] = $v3;
	}
	cache_write( 'power', 'member_group', $group );
}

/**
 * 缓存权限
 *
 */
function cache_power( $id = '' )
{
	global $db;
	$where = empty( $id )?'':'WHERE id='.$id;
	$sql = "SELECT `id`, `power` FROM `".DB_PRE."membergroup` $where";
	$group = $db->query( $sql );
	foreach( $group as $v )
	{
		$power = unserialize( $v['power'] );
		$file_name = 'group_'.$v['id'];
		cache_write( 'power', $file_name, $power );
	}
}

//缓存关键词
function cache_keywords()
{
	global $db;
	$datas = $db->get( DB_PRE.'keywords' );
	cache_write( 'article', 'keywords', $datas );
	unset( $datas );
}

/**
 * 缓存系统配置
 *
 */
function cache_sys_config()
{
	global $db;
	$datas = $db->get( 'sysconfig' );
	cache_write( 'config', 'sysconfig', $datas[0] );
	unset( $datas );
}

/**
 * 写缓存到文本
 *
 * @param string $file
 * @param array $datas
 * @return bool
 */



/**
 * 读取缓存
 *
 * @param string $file
 * @param int/null $id
 * @return array
 */
//function cache_read( $type, $file, $include = 'i' )
//{
//	$cachefile = SUN_CACHE.$type.'/'.$file.'.php';
//	if( !file_exists( $cachefile ) )
//	{
//		return array();
//	}
//	else
//	{
//		return $include == 'i' ? include $cachefile : file_get_contents( $cachefile );
//	}
//}
